/// Coder: Jose Ricardo Mello Viana - 2008
/// Bugs? e-mail me: zezim.ricardo@gmail.com

/**
 *
 * Author: Jose Ricardo (zezim)
 *
 * Data created: 16-05-08
 *
 **/

#ifndef COLORBUFFER_H_
#define COLORBUFFER_H_

#include "lal/Point3.hpp"
#include "lal/Vector3.hpp"
#include "lal/Color.hpp"
#include "glslKernel/glslKernel.h"
#include "paintCursor.h"

#include <vector>
#include <queue>
#include <GL/glut.h>

#define PI 3.1415926535897932384626433832795

using namespace LAL;
using namespace std;

typedef Point3<double> Point;
typedef Point3<int> iPoint;
typedef Vector3<double> Vector;
typedef Vector3<int> iVector;

class ColorBuffer {
public:
   /// Default constructor
   ColorBuffer();

   /// Constructor parsing cursor
   /// @param p given paintcursor
   /// @param w given width of buffer
   /// @param h given height of buffer
   ColorBuffer(PaintCursor *p, int w, int h);

   /// Default destructor
   virtual ~ColorBuffer();

   /// Paint current cursor position
   void paint();

   /// Paint cursor format
   /// @param p given center cursor point
   /// @param c given color
   void paintCursor(iPoint p, Color c);

   /// Classic Flood Fill algorithm
   /// @param p given point
   /// @param c given replacement color
   void floodFill(iPoint p, Color c);

   /// Do not paint anymore
   void unpaint();

   /// Clear temporary buffer flags
   void clearTemp();

   /// Draw the paint buffer
   void draw();

   ///
   void draw2();
   void textureTest();
   bool inModel(iPoint px);
   void updateStrokeBounds(iPoint pt);

   /// Classic Bresenham algorithm
   /// @param sr given source point
   /// @param tg given target point
   /// @return point between source and target
   vector<iPoint> bresenham(iPoint sr, iPoint tg);

private:
   PaintCursor *pc;
   int bh, bw;

   iPoint previous;
   bool havePrevious;

   vector<Color> buffer;
   vector<bool> isPainted;
   vector<bool> tmpPainted;
   vector<int> tmpIdx;

   vector<iVector> cursorvecs;
   iPoint mini, maxi;

   GLdouble cursor_format;

   glslKernel *shader_buffer;
   GLuint texPositions;

   iPoint sMin, sMax;
   bool bdSetted;
};

#endif /*COLORBUFFER_H_*/
